#!/usr/bin/env php
<?php
include_once(dirname(__FILE__).'/config.php');
include_once(dirname(__FILE__).'/utilities.php');

$store = ARC2::getStore($arc_config);

$time = time();

$q = <<<EOT
PREFIX vocabs: <http://desanti.org/schemas/vocabularies#>
SELECT * WHERE {
    ?v a vocabs:Vocabulary .
}
EOT;
$rs = $store->query($q);
if(!$store->getErrors()){
	$rows = $rs['result']['rows'];
    foreach($rows as $row){
        $time += 1;
        $vs[$row['v']] = 'http://desanti.org/vocabularies/'.$time;
    }
}

$q = <<<EOT
PREFIX vocabs: <http://desanti.org/schemas/vocabularies#>
SELECT * WHERE {
    ?v a vocabs:Vocabulary .
    ?v ?p ?o .
}
EOT;
$rs = $store->query($q);
if(!$store->getErrors()){
	$rows = $rs['result']['rows'];
    foreach($rows as $row){
        $newS = '<'.$vs[$row['v']].'>';
        $s = label($row,'v');
        $p = label($row,'p');
        $o = label($row,'o');
        $q = <<<EOT
PREFIX vocabs: <http://desanti.org/schemas/vocabularies#>
INSERT INTO <http://desanti.org/> {
    $newS $p $o .
}
EOT;
        query($q, $store);
        $q = <<<EOT
DELETE {
    $s $p $o .
}
EOT;
        query($q, $store);
    }
}

$q = <<<EOT
PREFIX vocabs: <http://desanti.org/schemas/vocabularies#>
SELECT * WHERE {
    ?v a vocabs:Vocabulary .
    ?s ?p ?v .
}
EOT;
$rs = $store->query($q);
if(!$store->getErrors()){
	$rows = $rs['result']['rows'];
    foreach($rows as $row){
        $newO = '<'.$vs[$row['v']].'>';
        $s = label($row,'s');
        $p = label($row,'p');
        $o = label($row,'v');
        $q = <<<EOT
PREFIX vocabs: <http://desanti.org/schemas/vocabularies#>
INSERT INTO <http://desanti.org/> {
    $s $p $newO .
}
EOT;
        query($q, $store);
        $q = <<<EOT
DELETE {
    $s $p $o .
}
EOT;
        query($q, $store);
    }
}

?>
